﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class MenuUIController : MonoBehaviour {
    public GameObject startCanvas;
	public GameObject levelCanvas;
	public GameObject loadingCanvas;
	public GameObject storyCanvas;
	private AsyncOperation mAsyncOperation;
	public string mainSceneName;
	public GameObject[] dialogs;
	public int currDialog = 0;
	// Use this for initialization
	void Start () {
		startCanvas.SetActive(true);
		//levelCanvas.SetActive(false);
		//loadingCanvas.SetActive(false);
		storyCanvas.SetActive(false);
	}
	
	// Update is called once per frame
	void Update () {
		
	}

	public void OnStartClick() {
		startCanvas.SetActive(false);
		storyCanvas.SetActive(true);
	}

	public void OnLevelSelected(int i) {
		//设置GameController中的关卡参数
        StartCoroutine(LoadScene());
		levelCanvas.SetActive(false);
		loadingCanvas.SetActive(true);
	}

	public void OnExitClick() {
		Application.Quit();
	}

	private IEnumerator LoadScene () {
    // u3d 5.3之后使用using UnityEngine.SceneManagement;加载场景
        mAsyncOperation = SceneManager.LoadSceneAsync("AsanCai");
    // 不允许加载完毕自动切换场景，因为有时候加载太快了就看不到加载进度条UI效果了
        mAsyncOperation.allowSceneActivation = true;
        while (!mAsyncOperation.isDone && mAsyncOperation.progress < 0.8f)
        {
            yield return mAsyncOperation;
        }
		mAsyncOperation.allowSceneActivation = true;
    }

	public void OnDialogClick() {
		dialogs[currDialog].GetComponent<Animator>().SetBool("OnClickNext", true);
		if(currDialog +1 == dialogs.Length) {
			Invoke("ShowLevel", 0.3f);
		} else Invoke("ShowNextDialog", 0.3f);
	}
	private void ShowNextDialog() {
		dialogs[currDialog+1].SetActive(true);
		currDialog += 1;
	}
	private void ShowLevel() {
		SceneManager.LoadSceneAsync("LevelChoose");
	}
}
